﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace ManageSystem
{
    static class Utils
    {
        static string conString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=ManageSystem;Integrated Security=True";
        static SqlConnection sqlcon = new SqlConnection(conString);

        public static SqlConnection getSqlCon()
        {
            if (sqlcon.State != System.Data.ConnectionState.Open)
            {
                sqlcon.Open();
                //if (sqlCon.State == System.Data.ConnectionState.Open)
                //    return true;
                //else
                //    return false;

                return sqlcon;
            }
            return sqlcon;


        }

        public static void CreateConnection()
        {
            if (sqlcon.State != System.Data.ConnectionState.Open)
            {
                sqlcon.Open();
            }
            else
            {
                CloseConnection();
                sqlcon.Open();
            }
        }

        public static void CloseConnection()
        {
            sqlcon.Close();
        }

        public static SqlCommand Querry(string strsql)
        {
            try
            {
                CreateConnection();
                SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
                sqlcmd.CommandText = strsql;
                return sqlcmd;
            }
            catch
            {
                return null;
            }
        }

        public static SqlDataReader getDataReader(string strsql)
        {
            try
            {
                CreateConnection();
                SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
                sqlcmd.CommandText = strsql;
                SqlDataReader sqlDr = sqlcmd.ExecuteReader();
                return sqlDr;
            }
            catch
            {
                return null;
            }
        }
        //执行Insert/update/delete，不带参数
        public static int ExecuteNoQuery(string strsql)
        {
            int i;

            try
            {
                CreateConnection();
                SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
                i = sqlcmd.ExecuteNonQuery();
                sqlcon.Close();
                return i;
            }
            catch
            {
                return -1;
            }
        }

        //执行Insert/update/delete，带参数
        public static int ExecuteNoQuery(string strsql, params SqlParameter[] param)
        {
            int i;
            try
            {
                CreateConnection();
                SqlCommand sqlcmd = new SqlCommand(strsql, sqlcon);
                //sqlcmd.Parameters.Add(param);
                foreach (SqlParameter par in param)   //遍历数组将参数对象添加到操作命令中
                {
                    sqlcmd.Parameters.Add(par);
                }
                i = sqlcmd.ExecuteNonQuery();
                return i;
            }
            catch
            {
                return -1;
            }
        }
        public static DataSet GetDataSet(string strsql)
        {
            CreateConnection();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
            DataSet ds = new DataSet();
            try
            {
                sda.Fill(ds);
                return ds;
            }
            catch
            {
                return null;
            }
        }
        public static DataSet GetDataSet(string strsql, params SqlParameter[] param)
        {
            CreateConnection();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
            DataSet ds = new DataSet();
            foreach (SqlParameter par in param)
            {
                sda.SelectCommand.Parameters.Add(par);

            }
            try
            {
                sda.Fill(ds);
                return ds;
            }
            catch
            {
                return null;
            }
        }
        public static DataTable GetTable(string strsql)
        {
            try
            {
                CreateConnection();
                SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
                DataSet ds = new DataSet();
                sda.Fill(ds, "temp");
                sqlcon.Close();
                return ds.Tables["temp"];
            }
            catch
            {
                return null;
            }
        }
        public static DataTable GetTable(string strsql, params SqlParameter[] param)
        {
            try
            {
                CreateConnection();
                string str = strsql;
                SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlcon);
                foreach (SqlParameter par in param)
                {
                    sda.SelectCommand.Parameters.Add(par);

                }
                DataSet ds = new DataSet();
                sda.Fill(ds, "temp");
                sqlcon.Close();
                return ds.Tables["temp"];

            }
            catch
            {
                return null;
            }


        }
    }
}
